package com.zyx.springretry.common.resp;


import com.zyx.springretry.common.exception.code.IErrorCode;
import com.zyx.springretry.common.exception.code.ResultCode;
import com.zyx.springretry.common.resp.base.BaseResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author Yaxi.Zhang
 * @since 2022/10/1 08:32
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResponse<T> extends BaseResponse implements Serializable {

    private T data;

    public CommonResponse(Integer code, String message) {
        this(code, message, null);
    }

    public CommonResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功返回结果
     */
    public static <T> CommonResponse<T> success(T data) {
        return new CommonResponse<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     */
    public static <T> CommonResponse<T> success(T data, String message) {
        return new CommonResponse<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResponse<T> failed(IErrorCode errorCode) {
        return new CommonResponse<>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResponse<T> failed(IErrorCode errorCode, String message) {
        return new CommonResponse<>(errorCode.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResponse<T> failed(String message) {
        return new CommonResponse<>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResponse<T> failed(Integer code, String message) {
        return new CommonResponse<>(code, message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResponse<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResponse<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResponse<T> validateFailed(String message) {
        return new CommonResponse<>(ResultCode.VALIDATE_FAILED.getCode(),
                String.format("%s,%s", message, ResultCode.VALIDATE_FAILED.getMessage()), null);
    }

    /**
     * 身份验证失败返回结果
     */
    public static <T> CommonResponse<T> authenticationFailed(String message) {
        return new CommonResponse<T>(ResultCode.AUTHENTICATION_FAILED.getCode(),
                String.format("%s,%s", message, ResultCode.AUTHENTICATION_FAILED.getMessage()), null);
    }

    /**
     * Token已经过期
     */
    public static <T> CommonResponse<T> authenticationExpired(String message) {
        return new CommonResponse<>(ResultCode.UNAUTHORIZED.getCode(), String.format("%s,%s", message, ResultCode.UNAUTHORIZED.getMessage()), null);
    }


    /**
     * Token无效返回结果
     */
    public static <T> CommonResponse<T> unauthorized(T data) {
        return new CommonResponse<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResponse<T> forbidden(T data) {
        return new CommonResponse<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }
}
